package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("mobile/order")
@Slf4j
public class MobileOrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SetMealService setMealService;

    /**
     * 预约
     * @param orderSubmitParam
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {
        log.info("[预约信息]date:{}", orderSubmitParam);

        //验证码对比(调用smsServiceImpl中验证码校验方法，把前端获取的type，手机号还有验证码传进去)
        // 如果校验结果不正确返回错误提示
        // smsService.checkValidateCode如果返回的是true就是相同，应该继续走后面代码，所以判断加个!
        if (!smsService.checkValidateCode("ORDER", orderSubmitParam.getTelephone(), orderSubmitParam.getValidateCode())) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

        //不是会员需要自动注册成会员
        //创建会员
        Member member = new Member();
        member.setName(orderSubmitParam.getName());//名字
        member.setSex(orderSubmitParam.getSex());//性别
        member.setPhoneNumber(orderSubmitParam.getTelephone());//手机号
        member.setIdCard(orderSubmitParam.getIdCard());//身份证号
        member = memberService.createOrUpdate(member);

        //提交体检信息
        Order order = new Order();
        order.setOrderStatus(Order.ORDERSTATUS_NO);//是否到诊
        order.setSetmealId(orderSubmitParam.getSetMealId());//套餐信息
        order.setOrderType(Order.ORDERTYPE_WEIXIN);//预约方式
        order.setOrderDate(orderSubmitParam.getOrderDate());//预约时间
        order.setMemberId(member.getId());//会员id
        order = orderService.add(order);

        return new Result(true, MessageConst.ORDER_SUCCESS,order);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        //查询数据
        Order order = orderService.findById(id);
        Member member = memberService.findById(order.getMemberId());
        Setmeal setmeal = setMealService.findById(order.getSetmealId());

        //封装返回结果
        Map<String,Object> map = new HashMap<>();
        map.put("member",member.getName());
        map.put("setmeal",setmeal.getName());
        map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
        map.put("orderType",order.getOrderType());
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
    }
}
